/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.property;

import java.util.ArrayList;

import org.openaecfm.model.property.CurveInterpolationEnum;
import org.openaecfm.model.property.Property;
import org.openaecfm.model.property.PropertyTableValue;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.io.measure.ValueIO;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EntityInstanceNameParameter;
import org.openaecfm.spf.model.EnumerationParameter;
import org.openaecfm.spf.model.ListParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.SimpleEntityInstance;
import org.openaecfm.spf.model.StringParameter;

public final class PropertyTableValueIO extends
		AbstractEntityInstanceInputImpl<PropertyTableValue> implements
		IEntityInstanceOutput<PropertyTableValue> {
	private IEntityInstanceUtilInput<Property> utilInput;
	private IEntityInstanceUtilOutput<Property> utilOutput;

	public void setUtilOutput(IEntityInstanceUtilOutput<Property> utilOutput) {
		this.utilOutput = utilOutput;
	}

	private ValueIO valueIO;

	public void setValueIO(ValueIO valueIO) {
		this.valueIO = valueIO;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String exportSPF(PropertyTableValue pojo,
			IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance(
				"IFCPROPERTYTABLEVALUE", appDataManager.getName(pojo));

		utilOutput.exportSPF(pojo, ei, appDataManager);

		// Set DefiningValues
		if (pojo.getDefiningValues() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ListParameter dingvalues = new ListParameter();
			for (Object v : pojo.getDefiningValues()) {
				dingvalues.add(valueIO.exportSPF(v));
			}
			ei.addParameter(dingvalues);
		}

		// Set DefinedValues
		if (pojo.getDefinedValues() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ListParameter dedvalues = new ListParameter();
			for (Object v : pojo.getDefinedValues()) {
				dedvalues.add(valueIO.exportSPF(v));
			}
			ei.addParameter(dedvalues);
		}

		// Set Expression
		if (pojo.getExpression() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			StringParameter s = new StringParameter(pojo.getExpression(),
					StringParameter.UTF_8);
			ei.addParameter(s);
		}

		// Set DefiningUnit
		if (pojo.getDefiningUnit() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			EntityInstanceNameParameter n = new EntityInstanceNameParameter(
					appDataManager.getName(pojo.getDefiningUnit()));
			ei.addParameter(n);
		}

		// Set DefinedUnit
		if (pojo.getDefinedUnit() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			EntityInstanceNameParameter n = new EntityInstanceNameParameter(
					appDataManager.getName(pojo.getDefinedUnit()));
			ei.addParameter(n);
		}

		// Set CurveInterpolation
		if (pojo.getCurveInterpolation() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new EnumerationParameter(pojo
					.getCurveInterpolation()));
		}

		return ei.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected PropertyTableValue doCreateObject() {
		return new PropertyTableValue();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveReferenceParameter(EntityInstance ei,
			PropertyTableValue pojo, IAppDataManager appDataManager) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set DefiningUnit
		Parameter dingunit = sei.getParameter(5);
		if (dingunit.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
			EntityInstanceNameParameter n = (EntityInstanceNameParameter) dingunit;
			Object entity = appDataManager.getObject(n.toInstanceName());
			pojo.setDefiningUnit(entity);
		}

		// Set DefinedUnit
		Parameter dedunit = sei.getParameter(6);
		if (dedunit.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
			EntityInstanceNameParameter n = (EntityInstanceNameParameter) dedunit;
			Object entity = appDataManager.getObject(n.toInstanceName());
			pojo.setDefinedUnit(entity);
		}
	}

	public void setUtilInput(IEntityInstanceUtilInput<Property> utilInput) {
		this.utilInput = utilInput;
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei,
			PropertyTableValue pojo) {
		utilInput.resolveNotReferenceParameter(ei, pojo);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set DefiningValues
		Parameter dingvalues = sei.getParameter(2);
		if (dingvalues.getNodeType() == NodeType.LIST) {
			if (pojo.getDefiningValues() == null) {
				pojo.setDefiningValues(new ArrayList<Object>());
			}
			ListParameter l = (ListParameter) dingvalues;
			for (Parameter p : l) {
				pojo.getDefiningValues().add(valueIO.importSPF(p));
			}
		}

		// Set DefinedValues
		Parameter dedvalues = sei.getParameter(3);
		if (dedvalues.getNodeType() == NodeType.LIST) {
			if (pojo.getDefinedValues() == null) {
				pojo.setDefinedValues(new ArrayList<Object>());
			}
			ListParameter l = (ListParameter) dedvalues;
			for (Parameter p : l) {
				pojo.getDefinedValues().add(valueIO.importSPF(p));
			}
		}

		// Set Expression
		Parameter expression = sei.getParameter(4);
		if (expression.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) expression;
			pojo.setExpression(s.toUTF8());
		}

		// Set CurveInterpolation
		Parameter curveInterpolation = sei.getParameter(7);
		if (curveInterpolation.getNodeType() == NodeType.ENUMERATION) {
			EnumerationParameter e = (EnumerationParameter) curveInterpolation;
			pojo.setCurveInterpolation(CurveInterpolationEnum.valueOf(e
					.toEnumString()));
		}
	}
}
